package com.exercise.widgets.dialog

import android.content.Intent
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentTransaction
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel
import com.exercise.widgets.MainActivity
import com.exercise.widgets.R
import com.exercise.widgets.databinding.ActivityDialogBinding
import com.exercise.widgets.dialog.dialogfragment.CustomDialogFragment
import com.exercise.widgets.dialog.dialogfragment.FullScreenDialogFragment
import com.exercise.widgets.dialog.dialogfragment.ListDialogFragment
import com.exercise.widgets.dialog.dialogfragment.StartGameDialogFragment

class DialogActivity : BaseActivity<BaseViewModel, ActivityDialogBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivityDialogBinding
        get() = ActivityDialogBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java

    override fun initData() {
    }

    override fun initView() {
        binding.startGameDialog.setOnClickListener {
            StartGameDialogFragment().show(supportFragmentManager, "startGame")
        }
        binding.listDialog.setOnClickListener {
            ListDialogFragment().show(supportFragmentManager, "listDialog")

        }
        binding.customDialog.setOnClickListener {
            CustomDialogFragment().show(supportFragmentManager, "customDialog")
        }
        binding.fullScreenDialog.setOnClickListener {
            val isLargeLayout = resources.getBoolean(R.bool.large_layout)
            showDialog(isLargeLayout)
        }
    }

    private fun showDialog(isLargeLayout: Boolean) {
        val fragmentManager = supportFragmentManager
        val newFragment = FullScreenDialogFragment()
        if (isLargeLayout) {
            newFragment.show(fragmentManager, "dialog")
        } else {
            val transaction = fragmentManager.beginTransaction()
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            transaction
                .add(R.id.content, newFragment)
                .addToBackStack(null)
                .commit()
        }

    }

    companion object {
        fun start(act: AppCompatActivity) {
            Intent(act, DialogActivity::class.java).also {
                act.startActivity(it)
            }
        }
    }

}